package com.cl.roadshow.zookeeper.chapter05;

import java.util.concurrent.CountDownLatch;

import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.Watcher.Event.KeeperState;
import org.apache.zookeeper.ZooKeeper;

//Chapter: 5.3.1 Java API -> 创建连接 -> 创建一个最基本的ZooKeeper对象实例
public class ZooKeeper_Constructor_Usage_Simple implements Watcher {
  private static CountDownLatch connectedSemaphore = new CountDownLatch(1);
  
  public static void main(String[] args) throws Exception{
      
      ZooKeeper zookeeper = new ZooKeeper("domain1.book.zookeeper:2181", 
      									5000, //
      									new ZooKeeper_Constructor_Usage_Simple());
      System.out.println(zookeeper.getState());
      try {
          connectedSemaphore.await();
      } catch (InterruptedException e) {}
      System.out.println("ZooKeeper session established.");
  }
  public void process(WatchedEvent event) {
      System.out.println("Receive watched event：" + event);
      if (KeeperState.SyncConnected == event.getState()) {
          connectedSemaphore.countDown();
      }
  }
}